<?php


namespace app\admin\controller;


use app\common\controller\admin\Base;
use app\admin\model\AppBase;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

class Appbaseconfig extends Base
{

    protected $noNeedLogin = [];

    protected $model;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\AppBaseConfig();
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        if ($this->request->isPost()) {
            $page = $this->request->post('page', 1);
            $size = $this->request->post('page_size', 10);
            $sort = $this->request->post('sort', 'id');
            $order = $this->request->post('order', 'desc');
            $adminId = $this->request->post('admin_id', '');
            $appBaseId = $this->request->post('app_base_id', '');
            $country_config_id = $this->request->post('country_config_id', '');

            if ($adminId != '') {
                $where['admin_id'] = $adminId;
            }
            if ($appBaseId != '') {
                $where['app_base_id'] = $appBaseId;
            }
            if ($country_config_id != '') {
                $where['country_config_id'] = $country_config_id;
            }

            $where['is_del'] = 0;
            $list = $this->model
                ->field('*')
                ->where($where)
                ->page($page, $size)
                ->order($sort, $order)
                ->select();
            if (!empty($list)) {

                $appBaseAll = AppBase::select()->toArray();

                $appBaseAll = array_column($appBaseAll, null, 'id');
                foreach ($list as $key => $value) {
                    $value['app_base_name'] = empty($appBaseAll[$value['app_base_id']]) ? "默认" : $appBaseAll[$value['app_base_id']]['title'];
                    $list[$key] = $value;
                }
            }
            $total = $this->model->where($where)->count('id');
            $totalPage = ceil($total / $size);
            $data = [
                'total' => $total,
                'list' => $list,
                'totalPage' => $totalPage
            ];
            $this->success('列表', '', $data);
        }
    }

    /**
     * 详情
     */
    public function details($app_base_id = 0,$country_config_id = 1)
    {
        $row = $this->model->where(['app_base_id' => $app_base_id])->where('country_config_id',$country_config_id)->find();
        if (!$row) {
            $this->error('未查询到数据！', '', [], 404);
        }
        $this->success('详情数据', '', $row);

    }

    /**
     * 新增或者编辑
     */
    public function insertOrUpdate()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {
                    $params['admin_id'] = $this->admin['id'];
                    $row = $this->model->where('app_base_id', $params['app_base_id'])->where('country_config_id',$params['country_config_id'])->find();
                    //如果查无此数据则新增有则编辑
                    if (empty($row)) {
                        $result = $this->model->allowField(true)->save($params);
                    } else {
                        $result = $row->allowField(true)->isUpdate(true)->save($params);
                    }
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
            $this->error('参数不能为空！');
        }
    }

}